package com.hao.tool.web.module.navigation.controller;

import com.hao.tool.common.entity.LayResponse;
import com.hao.tool.web.common.constant.Commons;
import com.hao.tool.web.module.navigation.dto.ModelTypeDTO;
import com.hao.tool.web.module.navigation.entity.ModelTypeInfo;
import com.hao.tool.web.module.navigation.service.ModelTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 导航栏
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2021-03-03 21:47
 */
@RestController
@RequestMapping("/rest/modelType")
public class ModelTypeRestController {

    @Autowired
    private ModelTypeService modelTypeService;


    @PostMapping("/add")
    public LayResponse add(ModelTypeDTO dto, String userName, HttpServletRequest request) {
        userName = (String) request.getAttribute(Commons.USER_NAME);
        ModelTypeInfo dbInfo = modelTypeService.findByModelMark(dto.getModelMark());
        if (dbInfo != null) {
            LayResponse layResponse = new LayResponse<ModelTypeInfo>();
            layResponse.setCode(LayResponse.ERROR)
                    .setMsg("mark 重复")
                    .setData(dbInfo);
            return layResponse;
        }
        ModelTypeInfo info = modelTypeService.addModelType(dto, userName);
        LayResponse layResponse = new LayResponse<ModelTypeInfo>();
        layResponse.setCode(LayResponse.SUCCESS)
                .setData(info);
        return layResponse;
    }

}
